package com.sl.plugin.step.db;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ObjectUtil;
import com.fasterxml.jackson.core.type.TypeReference;
import com.sl.core.domain.dto.JsonMappingFieldDefine;
import com.sl.core.utils.JsonUtil;
import com.sl.core.engine.meta.ProcessCompDescriptorService;
import com.sl.core.engine.meta.impl.AbstractProcessCompDescriptor;
import com.sl.core.engine.meta.row.ProcessRowDescriptor;
import com.sl.core.engine.transfer.MappingFiledDefineToJoltDslTransfer;
import com.sl.core.engine.utils.BeanUtil;
import lombok.Getter;
import org.apache.hop.core.row.value.ValueMetaBigNumber;

import java.util.List;
import java.util.Map;

/**
 *
 */
@Getter
public class DbInsertCompDescriptor extends AbstractProcessCompDescriptor<
        DbInsertComp, DbInsertCompContext> implements ProcessCompDescriptorService {


//    @Override
//    protected ProcessCompService getComp() {
//        return new DbQueryComp(this);
//    }
//
//    @Override
//    public ProcessCompContextService getContext() {
//        return new DbQueryCompContext();
//    }

    @Override
    public void loadDescriptor(Map<String, Object> map) {
        String dataSourceId = BeanUtil.getStr(map, "output.dataSourceId");
        String schemaId = BeanUtil.getStr(map, "output.schemaId");
        String tableName = BeanUtil.getStr(map, "output.tableName");

        Map<String, Object> maps = MapUtil.get(map, "output", Map.class);

        List<Map<String, Object>> params = getListMapAttr(maps, "params");
        //找到对象关系
        List<JsonMappingFieldDefine> jsonMappingFieldDefine = JsonUtil.toBean(params, new TypeReference<List<JsonMappingFieldDefine>>() {
        });

        if (CollUtil.isNotEmpty(jsonMappingFieldDefine)) {
            //转移到了映射
            String transfer = MappingFiledDefineToJoltDslTransfer.transfer(jsonMappingFieldDefine);
            putAttr("jolt", transfer);
        }
        //设置返回值
        ProcessRowDescriptor rowDescriptor = buildResultProcessRowDescriptor(maps);
        if(ObjectUtil.isNull(rowDescriptor)){
            return;
        }
        rowDescriptor.setIsArray(true);
        ValueMetaBigNumber bigNumber = new ValueMetaBigNumber();
        bigNumber.setName("rows");
        bigNumber.setComments("受影响行数");
        rowDescriptor.addValueMeta(bigNumber);
    }



    @Override
    public ProcessRowDescriptor getResultDescriptor(ProcessRowDescriptor rowDescriptor) {
        ProcessRowDescriptor rowMeta = super.getResultRowDescriptor();
        return rowMeta;
    }

}
